<template>
    <div class="components-container">
        <!-- 编辑 -->
        <sweet-modal ref="dialog" class="dialog" :sweetModalMaxWidth="700">
            <h3>{{title}} </h3>
            <div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">部门</label>
                        <div class="layui-input-inline">
                            <el-select v-model="chushibumen" placeholder="请选择部门" :disabled="isshow">
                                <el-option v-for="item in bumen" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">值班人员</label>
                        <div class="layui-input-inline">
                            <el-select v-model="dutypeoples" multiple placeholder="请选择值班人员" ref="inner" :disabled="isshow">
                                <el-option v-for="item in QueryNolstaff" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">值班日期</label>
                        <div class="layui-input-inline">
                            <el-date-picker v-date v-model="regList.WAHDATE" type="date" placeholder="选择时间" :disabled="isshow"></el-date-picker>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item btnCenter">
                <el-button @click="cancels">取消</el-button>
                <el-button type="primary" @click="submit" :loading="this.$store.state.isloading" v-if="!isshow">提交</el-button>
            </div>
        </sweet-modal>
    </div>
</template>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
<script>
import sweetModal from "@/components/sweetModal";
import dataTable from "@/components/Table/DataTable";
import apiBase from "@/api/apiBase.js";
import { manage } from "@/mixins/managePage.js";
 import {
        formatDate,
        timeStringToDate,
        dateStringToDate
    } from "@/utils/date.js";
const APPLY = "申请";
const MODIFY = "修改";
const DELETE = "删除";
const INFO = "详情";
const END = "结束使用";
const BEG = "开始使用";
const REG = "登记";
const CONTINUE = "使用续存";
const TOBESTORED = "X";
export default {
    name: "assUseMain",
    mixins: [manage],
    data() {
        return {
            isINFO: true, //详情页面全部不能修改
            GetSbsType: [],
            GetSbsMode: [],
            ids: null,
            QueryNolstaff: [], //获取员工id
            regList: {
                WAHDATE: null,//值班日期
            },
            dutypeoples: [],//值班人员
            chushibumen: null,//部门
            bumen: [],
            isshow: false,
        };
    },
    methods: {
        open(order, id) {
            this.ids = id;
            this.order = order;
            this.dataRepeat();
            this.getbmdata();
            this.isshow = false;
            switch (this.order) {
                case APPLY:
                    this.title = "登记值班申请";
                    this.dataRepeat();
                    this.isINFO = false;
                    this.$refs.dialog.open();
                    break;
                case MODIFY:
                    this.title = "补贴设置修改";
                    // this.dataRepeat();
                    this.dataInfo(id);
                    this.isINFO = false;
                    this.$refs.dialog.open();
                    break;
                case INFO:
                    this.title = "登记值班详情";
                    this.dataInfo(id);
                    this.isshow = true;
                    this.$refs.dialog.open();
                    break;
                default:
            }
        },
        //提交
        submit() {
            this.$store.state.isloading = true;
            switch (this.order) {
                case APPLY:
                    this.dataAdd();
                    break;
                case MODIFY:
                    this.dataModify();
                    break;
                default:
            }
        },
        //补贴方式改变
        changesbsmode(val) {
            var val = this.regList.SBSTYPE;
            if (val === "A") {
                //固定值
            } else if (val === "B") {
                //百分比
                if (Number(this.regList.SBSMONEY) > 100) {
                    this.regList.SBSMONEY = 0;
                }
            }
        },
        //补贴金额改变
        changesbsmoney() {
            var val = this.regList.SBSMONEY;
            this.regList.SBSMONEY = this.regList.SBSMONEY.replace(/[^\d]/g, "");
            if (this.regList.SBSTYPE === "A") {
                //固定值
                if (Number(val) < 0) {
                    this.regList.SBSMONEY = 0;
                }
            } else if (this.regList.SBSTYPE === "B") {
                //百分比
                if (Number(val) < 0) {
                    this.regList.SBSMONEY = 0;
                } else if (Number(val) > 100) {
                    this.regList.SBSMONEY = 100;
                }
            } else {
                alert("请选择补贴方式！！");
            }
        },
        //获取补贴类型
        GetSbsTypes() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Code.GetSbsType,
                res => {
                    let assTypeData = res.data.content,
                        legendsUsersData = assTypeData.map(item => ({
                            label: item.CodeName,
                            value: item.CodeValue
                        }));
                    this.GetSbsType = legendsUsersData;
                }
            );
        },
        //获取补贴方式
        GetSbsModes() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Subsidy.GetSbsType,
                res => {
                    let assTypeData = res.data.content,
                        legendsUsersData = assTypeData.map(item => ({
                            label: item.Text,
                            value: item.Value
                        }));
                    this.GetSbsMode = legendsUsersData;
                }
            );
        },
        //获取部门名称
        // getbmname() {
        //     let data = {};
        //     apiBase.DoService(
        //         data,
        //         apiBase.getApiAdress().JointRegistration.GetDpeName,
        //         res => {
        //             var s = res.data.content.DPTNAME;
        //             this.chushibumen = s;
        //         }
        //     );
        // },
        getbmdata() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Dept.QueryDeptByNormal,
                res => {
                    var s = res.data.content.OutData;
                       var legendsUsersData = s.map(item => ({
                            label: item.DPTNAME,
                            value: item.DPTID
                        }));
                    this.bumen = legendsUsersData;
                }
            );
        },
        //获取员工id
        QueryNolstaffs() {
            let data = {};
            apiBase.DoService(
                data,
                apiBase.getApiAdress().Staff.QueryNolstaff,
                res => {
                    let assTypeData = res.data.content,
                        legendsUsersData = assTypeData.map(item => ({
                            label: item.STANAME,
                            value: item.STAID
                        }));
                    this.QueryNolstaff = legendsUsersData;
                }
            );
        },
        //数据重置
        dataRepeat() {
            this.regList = {
                WAHDATE: null,//值班日期
            };
            this.dutypeoples = [];
            this.chushibumen = null;
        },
        //新增
        dataAdd() {
            let peoples = [];
            let Data = JSON.parse(JSON.stringify(this.regList));
            for(var i = 0;i < this.dutypeoples.length;i++){
                for(var s = 0;s < this.QueryNolstaff.length;s++){
                    if(this.dutypeoples[i] === this.QueryNolstaff[s].value){
                        peoples.push(this.QueryNolstaff[s].label);
                    }
                }
            }
            Data.WAHDPTID = this.chushibumen;
            Data.WAHMANS = peoples.join(',');
            if(Data.WAHDATE){
                Data.WAHDATE = formatDate(Data.WAHDATE, "yyyyMMdd");
            }
            apiBase.DoService(
                Data,
                apiBase.getApiAdress().OnDuty.CreateWah,
                res => {
                    this.$message({
                        type: "success",
                        message: "新增成功!"
                    });
                    this.refreshData();
                    this.$refs.dialog.close();
                }
            );
        },
        //详情
        dataInfo(id) {
            var data = {
                WAHID: id
            };
            apiBase.DoService(
                data,
                apiBase.getApiAdress().OnDuty.DetailWah,
                res => {
                    let peopless = [];
                    this.regList.WAHDATE = res.data.content.WAHDATEDESC;
                    this.chushibumen = res.data.content.WAHDPTID;
                    let wahdatas = res.data.content.WAHMANS.split(',');
                    for(var i = 0;i <wahdatas.length;i++){
                        for(var s = 0;s < this.QueryNolstaff.length;s++){
                            if(wahdatas[i] === this.QueryNolstaff[s].label){
                                peopless.push(this.QueryNolstaff[s].value);
                            }
                        }
                    }
                    this.dutypeoples = peopless;
                }
            );
        },
        //修改
        dataModify() {
            let peoples = [];
            let Data = JSON.parse(JSON.stringify(this.regList));
            for(var i = 0;i < this.dutypeoples.length;i++){
                for(var s = 0;s < this.QueryNolstaff.length;s++){
                    if(this.dutypeoples[i] === this.QueryNolstaff[s].value){
                        peoples.push(this.QueryNolstaff[s].label);
                    }
                }
            }
            Data.WAHDPTID = this.chushibumen;
            Data.WAHMANS = peoples.join(',');
            if(Data.WAHDATE){
                Data.WAHDATE = formatDate(Data.WAHDATE, "yyyyMMdd");
            }
            Data.WAHID = this.ids;
            apiBase.DoService(
                Data,
                apiBase.getApiAdress().OnDuty.ModifyWah,
                res => {
                    this.$message({
                        type: "success",
                        message: "修改成功!"
                    });
                    this.refreshData();
                    this.$refs.dialog.close();
                }
            );
        },
        // 页面关闭
        cancels() {
            this.$refs.dialog.close();
        },
        refreshData() {
            this.$emit("refreshData");
        }
    },
    created() {
        this.GetSbsTypes();
        this.GetSbsModes();
        this.QueryNolstaffs();
    },
    components: {
        sweetModal
    }
};
</script>

